package cn.tedu.coolshark.controller;

import cn.tedu.coolshark.entity.Category;
import cn.tedu.coolshark.mapper.CategoryMapper;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

@RestController
public class CategoryController {

    @Autowired
    CategoryMapper mapper;


    @RequestMapping("/category/select")
    public List<Category> select(){
        return mapper.select();
    }

    @RequestMapping("/category/delete")
    public void delete(int id ){
        System.out.println("删除分类的id = " + id);
        mapper.deleteById(id);
    }

    @RequestMapping("/category/insert")
    public int insert(@RequestBody Category category){
        Category c = mapper.selectByName(category.getName());
        if (c!=null){
            return 1;
        }
        mapper.insert(category);
        return 2;
    }
}
